package com.blast.manager.uitl;

import com.wxl.common.room.entity.LeiGuanEntity;
import com.wxl.common.util.StringUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortUtil {

    // 区孔正序排序
    public static void sort(List<LeiGuanEntity> list) {
        Collections.sort(list, new Comparator<LeiGuanEntity>(){
            /**  
                    * int compare(DetonatorModel o1, DetonatorModel o2) 返回一个基本类型的整型，  
                    * 返回负数表示：o1 小于o2，  
                    * 返回0 表示：o1和o2相等，  
                    * 返回正数表示：o1大于o2。  
                    */
            public int compare(LeiGuanEntity o1, LeiGuanEntity o2) {
                String hexString1 = StringUtils.HexToBin16(o1.holeNumber);
                String hexString2 = StringUtils.HexToBin16(o2.holeNumber);
//                if (Integer.parseInt(hexString1.substring(0, 3), 2) > Integer.parseInt(hexString2.substring(0, 3), 2)) {
//                    return 1;
//                }
//                if (Integer.parseInt(hexString1.substring(0, 3), 2) == Integer.parseInt(hexString2.substring(0, 3), 2)) {
                    return sortRowNumber(hexString1, hexString2);
//                }
//                return -1;
            }
        });
    }

    private static int sortRowNumber(String hexString1, String hexString2) {
        if(StringUtils.getRowNumber(hexString1) > StringUtils.getRowNumber(hexString2)){
            return 1;
        }
        if(StringUtils.getRowNumber(hexString1) > StringUtils.getRowNumber(hexString2)){
            return sortHoleNumber(hexString1, hexString2);
        }
        return -1;
    }

    private static int sortHoleNumber(String hexString1, String hexString2) {
        if(StringUtils.getHoleNumber(hexString1) > StringUtils.getHoleNumber(hexString2)){
            return 1;
        }
        if(StringUtils.getHoleNumber(hexString1) > StringUtils.getHoleNumber(hexString2)){
            return 0;
        }
        return -1;
    }

    // 区孔倒序排序
    public static void reverseSort(List<LeiGuanEntity> list) {
        Collections.sort(list, new Comparator<LeiGuanEntity> (){
            /**  
                    * int compare(DetonatorModel o1, DetonatorModel o2) 返回一个基本类型的整型，  
                    * 返回负数表示：o1 小于o2，  
                    * 返回0 表示：o1和o2相等，  
                    * 返回正数表示：o1大于o2。  
                    */
            public int compare(LeiGuanEntity o1, LeiGuanEntity o2) {
                String hexString1 = StringUtils.HexToBin16(o1.holeNumber);
                String hexString2 = StringUtils.HexToBin16(o2.holeNumber);
//                if (Integer.parseInt(hexString1.substring(0, 3), 2) > Integer.parseInt(hexString2.substring(0, 3), 2)) {
//                    return -1;
//                }
//                if (Integer.parseInt(hexString1.substring(0, 3), 2) == Integer.parseInt(hexString2.substring(0, 3), 2)) {
                    return sortReverseRowNumber(hexString1, hexString2);
//                }
//                return 1;
            }
        });
    }

    private static int sortReverseRowNumber(String hexString1, String hexString2) {
        if(StringUtils.getRowNumber(hexString1) > StringUtils.getRowNumber(hexString2)){
            return -1;
        }
        if(StringUtils.getRowNumber(hexString1) > StringUtils.getRowNumber(hexString2)){
            return sortReverseHoleNumber(hexString1, hexString2);
        }
        return 1;
    }

    private static int sortReverseHoleNumber(String hexString1, String hexString2) {
        if(StringUtils.getHoleNumber(hexString1) > StringUtils.getHoleNumber(hexString2)){
            return -1;
        }
        if(StringUtils.getHoleNumber(hexString1) > StringUtils.getHoleNumber(hexString2)){
            return 0;
        }
        return 1;
    }

    // 延时正序排序
    public static void delaySort(List<LeiGuanEntity> list) {
        Collections.sort(list, new Comparator<LeiGuanEntity> (){
            /**  
                    * int compare(DetonatorModel o1, DetonatorModel o2) 返回一个基本类型的整型，  
                    * 返回负数表示：o1 小于o2，  
                    * 返回0 表示：o1和o2相等，  
                    * 返回正数表示：o1大于o2。  
                    */
            public int compare(LeiGuanEntity o1, LeiGuanEntity o2) {
                if(Integer.parseInt(o1.delay) > Integer.parseInt(o2.delay)){
                    return 1;
                }
                if(o1.delay.equals(o2.delay)){
                    return 0;
                }
                return -1;
            }
        });
    }

    // 延时倒序排序
    public static void delayReverseSort(List<LeiGuanEntity> list) {
        Collections.sort(list, new Comparator<LeiGuanEntity> (){
            /**  
                    * int compare(DetonatorModel o1, DetonatorModel o2) 返回一个基本类型的整型，  
                    * 返回负数表示：o1 小于o2，  
                    * 返回0 表示：o1和o2相等，  
                    * 返回正数表示：o1大于o2。  
                    */
            public int compare(LeiGuanEntity o1, LeiGuanEntity o2) {
                if(Integer.parseInt(o1.delay) > Integer.parseInt(o2.delay)){
                    return -1;
                }
                if(o1.delay.equals(o2.delay)){
                    return 0;
                }
                return 1;
            }
        });
    }

    // 编码正序排序
    public static void codeSort(List<LeiGuanEntity> list) {
        Collections.sort(list, new Comparator<LeiGuanEntity> (){
            /**  
                    * int compare(DetonatorModel o1, DetonatorModel o2) 返回一个基本类型的整型，  
                    * 返回负数表示：o1 小于o2，  
                    * 返回0 表示：o1和o2相等，  
                    * 返回正数表示：o1大于o2。  
                    */
            public int compare(LeiGuanEntity o1, LeiGuanEntity o2) {
                if (o1.UID.length() >= 16 && o2.UID.length() >= 16) {
                    if (Integer.valueOf(o1.UID.substring(10, 16), 16) > Integer.valueOf(o2.UID.substring(10, 16), 16)) {
                        return 1;
                    }
                    if (o1.UID.equals(o2.UID)) {
                        return 0;
                    }
                }
                return -1;
            }
        });
    }

    // 编码倒序排序
    public static void codeReverseSort(List<LeiGuanEntity> list) {
        Collections.sort(list, new Comparator<LeiGuanEntity> (){
            /**  
                    * int compare(DetonatorModel o1, DetonatorModel o2) 返回一个基本类型的整型，  
                    * 返回负数表示：o1 小于o2，  
                    * 返回0 表示：o1和o2相等，  
                    * 返回正数表示：o1大于o2。  
                    */
            public int compare(LeiGuanEntity o1, LeiGuanEntity o2) {
                if (o1.UID.length() >= 16 && o2.UID.length() >= 16) {
                    if (Integer.valueOf(o1.UID.substring(10, 16), 16) > Integer.valueOf(o2.UID.substring(10, 16), 16)) {
                        return -1;
                    }
                    if (o1.UID.equals(o2.UID)) {
                        return 0;
                    }
                }
                return 1;
            }
        });
    }
}
